package com.dingup.provider.feign.server.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dingup.api.adapter.user.SysUserAdapter;
import com.dingup.api.bean.response.SelectBean;
import com.dingup.api.model.sys.SysDept;
import com.dingup.api.model.sys.SysRole;
import com.dingup.api.model.sys.SysUser;
import com.dingup.provider.mapper.SysUserMapper;
import com.dingup.provider.service.sys.SysDeptService;
import com.dingup.provider.service.sys.SysRoleService;
import com.dingup.provider.service.sys.SysUserService;
import com.dingup.provider.service.wx.SysWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by wancheng on 2018/9/27.
 */
@RestController()
@RequestMapping("/feign/user")
public class UserFeign {

    private final String ROLE_NAME = "学生";

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    SysWxUserService sysWxUserService;
    @Autowired
    SysDeptService sysDeptService;

    @RequestMapping(value = "/get_user_name_by_id.do", method = RequestMethod.POST)
    public String getUserNameByUserId(String userId) {
        return sysUserService.getUserNameById(userId);
    }

    @RequestMapping(value = "/get_all_user_name.do", method = RequestMethod.POST)
    public List<SelectBean> getAllUserName() {
        return sysUserService.getAllUserNameAndUserIds();
    }

    @RequestMapping(value = "/get_user_by_user_id.do", method = RequestMethod.POST)
    public SysUserAdapter getUserByUserId(String userId) {
        SysUser user = sysUserMapper.selectByPrimaryKey(userId);
        List<SysRole> roles = sysRoleService.getByUserId(userId);
        List<SysDept> depts = sysDeptService.getByUser(userId);
        SysUserAdapter adapter = JSONObject.parseObject(JSON.toJSONString(user),SysUserAdapter.class);
        Boolean flag = false;
        for (SysRole r : roles) {
            if (ROLE_NAME.equals(r.getName())){
                flag = true;
                break;
            }
        }
        adapter.setDepts(depts);
        adapter.setRoles(roles);
        adapter.setIfStudent(flag);
        return adapter;
    }

    @RequestMapping(value = "/get_user_wx_open_id.do", method = RequestMethod.POST)
    public String getWxOpenId(String userId) {
        return sysWxUserService.getWxOpenId(userId);
    }

    @RequestMapping(value = "/get_parent_wx_open_id.do", method = RequestMethod.POST)
    public List<String> getParentWxOpenId(String userId) {
        return sysWxUserService.getParentWxOpenId(userId);
    }

    @RequestMapping(value = "/get_user_from_system.do", method = RequestMethod.POST)
    public List<SelectBean> getUserFromSystem() {
        return sysUserService.getUserFromSysten();
    }
}
